package com.wd.mylibrary_base.base

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.wd.mylibrary_base.utils.MyApp
import java.lang.reflect.ParameterizedType



/**
 * A simple [Fragment] subclass.
 * Use the [BaseFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
abstract class BaseFragment <V: ViewModel,D: ViewDataBinding>(private val layoutId:Int): Fragment() {

    lateinit var viewModel: V
    lateinit var dataBinding: D

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        dataBinding= DataBindingUtil.inflate(inflater,layoutId,container,false)
        dataBinding.lifecycleOwner=this
        return dataBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        var type=javaClass.genericSuperclass as ParameterizedType
        var  classVM=type.actualTypeArguments[0]as Class<V>
        viewModel= ViewModelProvider(this)[classVM]

        initView()
        dataObserve()
    }

    abstract fun initView()

    abstract fun dataObserve()

    override fun onDestroy() {
        super.onDestroy()

        if(dataBinding!=null){
            dataBinding.unbind()
        }
    }





}